package com.ng.framework.view.desktop.table;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

public class NGCheckBoxTableCellRenderer extends JCheckBox implements TableCellRenderer {
	
	private static final long serialVersionUID = 1741654071867373433L;

	private Border noFocusBorder;

	public NGCheckBoxTableCellRenderer() {
		this.noFocusBorder = new EmptyBorder(1, 2, 1, 2);
		this.setHorizontalAlignment(CENTER);
		this.setOpaque(true);
		this.setBorder(noFocusBorder);
	}
	
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		Color background = null;

		TableModel model = table.getModel();
		if (model instanceof NGTableModel) {
			NGCellAttributes cellAtt = ((NGTableModel) model).getCellAttributes();

			background = cellAtt.getCellBackground(row, column);
		}
		
		if (isSelected) {
			this.setBackground(table.getSelectionBackground());
		} else {
			if (row % 2 == 0) {
				this.setBackground((background != null) ? background : UIManager.getColor("Table.alternateRowColor"));
			} else {
				this.setBackground((background != null) ? background : new Color(table.getBackground().getRGB()));
			}
		}

		if (hasFocus) {
			setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
		} else {
			setBorder(noFocusBorder);
		}

		this.setSelected(value != null ? (Boolean)value : false);
		
		this.setEnabled(table.isCellEditable(row, column));
		
		return this;
	}

}
